"use strict";
/**
 * @ Author: Yoann Meclot. MSay2
 * @ Created on: 2020-08-28 12:21:31
 * @ Modified on: 2021-06-14 18:30:15
 *
 * @version 1.4
 */
/**
 * Copyright (c) 2020-2021 MSay2 (Yoann Meclot) - @msay2/tspoet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TSMethod = void 0;
const Exceptions_js_1 = require("../util/Exceptions.js");
const ArrayList_js_1 = require("../util/list/ArrayList.js");
const Arrays_js_1 = require("../util/list/Arrays.js");
const LinkedMap_js_1 = require("../util/list/LinkedMap.js");
const Utils_js_1 = require("../util/Utils.js");
const Modifier_js_1 = require("./Modifier.js");
/**
 * TSMethod object allows you to write a method.
 *
 * @class `TSMethod`
 * @version 1.3
 */
class TSMethod {
    constructor(builder) {
        this.mImports = builder.mImports;
        this.mName = builder.mName;
        this.mType = Utils_js_1.Preconditions.checkNotNull(builder.mType, "method return type", "type");
        this.mAnnotations = builder.mAnnotations;
        this.mModifiers = builder.mModifiers;
        this.mStatments = builder.mStatements;
        this.mParameters = builder.mParameters;
        this.mCodeBlocks = builder.mCodeBlocks;
        this.mDocs = builder.mDocs;
        this.mGenericNames = builder.mGenericNames;
    }
    /**
     * Build constructor method.
     *
     * @type {TSMethod.Builder}
     * @since 1.0
     */
    static constructorBuilder() {
        return new TSMethod.Builder(TSMethod.CONSTRUCTOR);
    }
    /**
     * Build method.
     *
     * @param name The name of the method.
     * @throws {TSPoetException} Check if the name is undefined or null.
     * @type {TSMethod.Builder}
     * @since 1.0
     */
    static methodBuilder(name) {
        return new TSMethod.Builder(name);
    }
    emit(codeWriter) {
        if (this.mModifiers.contains(Modifier_js_1.Modifier.ABSTRACT))
            codeWriter.emitAbstractMethod(this);
        else
            codeWriter.emitMethod(this);
        return codeWriter;
    }
}
exports.TSMethod = TSMethod;
TSMethod.CONSTRUCTOR = "msay2.tspoet:CONSTRUCTOR";
TSMethod.Builder = class Builder {
    constructor(name) {
        this.mImports = new ArrayList_js_1.ArrayList();
        this.mAnnotations = new ArrayList_js_1.ArrayList();
        this.mStatements = new ArrayList_js_1.ArrayList();
        this.mParameters = new LinkedMap_js_1.LinkedMap();
        this.mCodeBlocks = new ArrayList_js_1.ArrayList();
        this.mDocs = new ArrayList_js_1.ArrayList();
        this.mDomments = new ArrayList_js_1.ArrayList();
        this.mGenericNames = new ArrayList_js_1.ArrayList();
        this.mModifiers = new ArrayList_js_1.ArrayList();
        if (name == TSMethod.CONSTRUCTOR)
            name = "constructor";
        this.mName = Utils_js_1.Preconditions.checkNotNull(name, "method name", "name");
    }
    /**
     * Add a documentation.
     *
     * @param arg Your documentation.
     * @throws {TSPoetException} Check if the arg is undefined or null.
     * @type {TSMethod.Builder}
     * @since 1.2
     */
    addDoc(arg) {
        this.mDocs.add(Utils_js_1.Preconditions.checkNotNull(arg, "method documentation", "string argument"));
        return this;
    }
    /**
     * Add a annotation for the method.
     *
     * @param tsAnnotation The annotation to be added.
     * @throws {TSPoetException} If you is on contructor builder.
     * @throws {TSPoetException} Check if the annotation is undefined or null.
     * @type {TSMethod.Builder}
     * @since 1.0
     */
    addAnnotation(tsAnnotation) {
        if (this.mName == TSMethod.CONSTRUCTOR)
            throw new Exceptions_js_1.TSPoetException("Constructor cannot have a annotaion.");
        this.mAnnotations.add(Utils_js_1.Preconditions.checkNotNull(tsAnnotation, "method annotation", "TSAnnotation object"));
        for (let entry of tsAnnotation.mImports.entries())
            this.putImport(entry.getValue());
        return this;
    }
    /**
     * Add modifiers.
     *
     * @param modifiers The modifiers to be added.
     * @throws {TSPoetException} Check if the modifiers is undefined or null.
     * @throws {TSPoetException} If the modifiers contain the static, export, abstract modifiers for the constructor.
     * @type {TSMethod.Builder}
     * @since 1.0
     */
    addModifiers(...modifiers) {
        this.mModifiers = Arrays_js_1.Arrays.asList(Utils_js_1.Preconditions.checkNotNull(modifiers, "method modifiers", "modifiers"));
        if (this.mName == TSMethod.CONSTRUCTOR && !Modifier_js_1.Modifier.isConstructor(this.mModifiers))
            throw new Exceptions_js_1.TSPoetException("Constructor can contains only PUBLIC, PROTECTED, PRIVATE or nothing modifiers.");
        return this;
    }
    /**
     * Add parameter.
     *
     * @param name The name parameter.
     * @param objectName The parameter type.
     * @throws {TSPoetException} Check if the ObjectName object is undefined or null.
     * @throws {TSPoetException} Check if the name is undefined or null.
     * @type {TSMethod.Builder}
     * @since 1.0
     */
    addParameter(name, objectName) {
        let c = Utils_js_1.Preconditions.checkNotNull(objectName, "method parameter type", "type");
        this.mParameters.add(Utils_js_1.Preconditions.checkNotNull(name, "method parameter name", "name"), c);
        this.putImport(c);
        return this;
    }
    /**
     * Add statement.
     *
     * @param arg Your code line to be added.
     * @throws {TSPoetException} Check if the arg is undefined or null.
     * @type {TSMethod.Builder}
     * @since 1.0
     */
    addStatement(arg) {
        this.mStatements.add(Utils_js_1.Preconditions.checkNotNull(arg, "method statement", "string argument"));
        return this;
    }
    /**
     * Add code block.
     *
     * @param codeBlock Your code block to be added.
     * @throws {TSPoetException} Check if the code block is undefined or null.
     * @type {TSMethod.Builder}
     * @since 1.0
     */
    addCodeBlock(codeBlock) {
        let c = Utils_js_1.Preconditions.checkNotNull(codeBlock, "method code block", "CodeBlock object");
        this.mCodeBlocks.add(c);
        for (let entry of codeBlock.mInports.entries())
            this.putImport(entry.getValue());
        return this;
    }
    /**
     * Type cast the method.
     *
     * @param genericsNames The generics names.
     * @throws {TSPoetException} If you attempt cast the constructor.
     * @throws {TSPoetException} Check if the generics names is undefined or null.
     * @type {TSMethod.Builder}
     * @since 1.0
     */
    casts(...genericsNames) {
        if (this.mName == TSMethod.CONSTRUCTOR)
            throw new Exceptions_js_1.TSPoetException("Cannot cast constructor");
        this.mGenericNames = Arrays_js_1.Arrays.asList(Utils_js_1.Preconditions.checkNotNull(genericsNames, "generics names", "names"));
        return this;
    }
    /**
     * Set return type to method.
     *
     * @param objectName The return type.
     * @throws {TSPoetException} If you attempt to apply a return value to constructor.
     * @throws {TSPoetException} Check if the ObjectName object is undefined or null.
     * @type {TSMethod.Builder}
     * @since 1.0
     */
    return(objectName) {
        if (this.mName == TSMethod.CONSTRUCTOR)
            throw new Exceptions_js_1.TSPoetException("Constructor cannot have a return type.");
        this.mType = Utils_js_1.Preconditions.checkNotNull(objectName, "method type", "type");
        this.putImport(objectName);
        return this;
    }
    /**
     * Build a method.
     * @throws {TSPoetException} Check if the return type is undefined or null.
     * @type {TSMethod}
     * @since 1.0
     */
    build() {
        return new TSMethod(this);
    }
    putImport(ObjectName) {
        this.mImports.add(ObjectName);
    }
};
//# sourceMappingURL=TSMethod.js.map